package com.mike.server;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.log4j.Logger;

import com.mike.server.engine.GameEngine;
import com.mike.server.engine.socket.EngineSocketServer;
import com.mike.server.support.BaseServer;
import com.mike.server.system.chat.ChatService;
import com.mike.server.system.group.GroupService;
import com.mike.server.system.mail.MailService;
import com.mike.server.system.map.MapService;
import com.mike.server.util.GameMessageCreater;

public class GameServer extends BaseServer {

	private static final Logger LOG = Logger.getLogger(GameServer.class);
	
	private static GameServer instance;
	
	private static final Lock lock = new ReentrantLock();

	private boolean isInited = false;
	
	public boolean isInited() {
		return isInited;
	}

	private GameServer() {
		//instance Don't allow new it outside.
	}
	
	public static GameServer getInstanse() {
		//DCL
		if (null == instance) {
			if (lock.tryLock()) {
				try {
					if (null == instance) {
						instance = new GameServer();
					}
				} finally {
					lock.unlock();
				}
			}
		}
		return instance;
	}
	
	public static void main(String[] args) {
		GameServer.getInstanse().start();
		//others();
		LOG.info("服务器启动完毕");
	}

	private static void others() {
		GameMessageCreater.getInstance().start();
	}

	@Override
	public void shutdown() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void start() {
		initService();
		startService();
	}
	
	/**
	 * 所有模块的处理线程在这里启动
	 */
	private void initService() {
		ChatService.getInstance().init();
		GroupService.getInstance().init();
		MailService.getInstance().init();
		MapService.getInstance().init();
		isInited  = true;
	}
	
	private void startService() {
		GameEngine.getInstance().start();
		EngineSocketServer.getInstance().start();
	}
}
